package arrays;

public class P3 {

    public static void main(String[] args) {
        int[] ints = new int[] { 2, 3, 4, 5, 18, 17, 6 };
        System.out.println(maxArea2(ints));
    }

    public int maxArea(int[] height) {
        int res = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = i; j < height.length; j++) {
                res = Math.max((j - i) * Math.min(height[i], height[j]), res);
            }
        }
        return res;
    }

    public static int maxArea2(int[] height) {
        if (height.length == 1) {
            return 0;
        }
        int left = 0, right = height.length - 1;
        int size = 0;
        while (left < right) {
            size = Math.max((right - left) * Math.min(height[left], height[right]), size);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return size;
    }
}
